import { defineStore } from "pinia"
import { ref, computed } from "vue"
//pinia直接使用组合式
let useCounter = defineStore('counter', () => {
    //声明一个状态
    let count = ref(20);//相当于vuex中的state
    let msg = ref("hello world")
    let arr = ref(['吃饭', '睡觉', '打豆豆'])
    let product = ref([])
    //声明一个方法
    let increment = () => {//相当于vuex中的actions + mutations
        count.value++
    }

    //声明一个发送异步请求的方法,不管方法中是同步逻辑还异步逻辑,都能被调试工具检测到

    function fetch_data() {
        fetch('/api/data/index.json')
            .then(body => body.json())
            .then(res => {
                console.log(res)
                product.value = res.data;
            })
    }

    //相当于vuex中getters
    let reverseMsg = computed(() => {
        return msg.value.split('').reverse().join('')
    })

    return {
        count,
        increment,
        reverseMsg,
        msg,
        arr,
        fetch_data,
        product
    }

})


export default useCounter